package vue.panneau3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import vue.panneau1.Interface2DZone1;

import modele.FonctionAOptimiser2D;

public class PopupChoixFonctionAOptimiser2D extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private Interface2DZone1 z1;
	
	private JDialog fen;
	private JPanel pan;
	private Box boxH1,boxH2,boxV;
	private JLabel labelOpt,labelX1,labelX2;
	private JComboBox jcbChoixOpt;
	private JTextField jtfX1,jtfX2;
	private JButton bOk,bAnnuler;
	
	public PopupChoixFonctionAOptimiser2D(Interface2DZone1 z1)
	{
		this.z1 = z1;
		
		labelOpt = new JLabel("Optimiser :");
		labelX1 = new JLabel("X1 :");
		labelX2 = new JLabel("X2 :");	
		
		jcbChoixOpt = new JComboBox();
		jcbChoixOpt.insertItemAt("Minimiser",0);
		jcbChoixOpt.insertItemAt("Maximiser",1);
		
		jtfX1 = new JTextField(12);
		jtfX2 = new JTextField(12);
		
		jcbChoixOpt.setSelectedItem(z1.getOptChoix());
		jtfX1.setText(z1.getOptValeurX1X2("x1"));
		jtfX2.setText(z1.getOptValeurX1X2("x2"));
		
		bOk = new JButton("OK");
		bAnnuler = new JButton("Annuler");
		
		bOk.setMaximumSize(bAnnuler.getMinimumSize());
		
		bOk.setActionCommand("ok");
		bAnnuler.setActionCommand("annuler");
		
		bOk.addActionListener(this);		
		bAnnuler.addActionListener(this);
		
		pan = new JPanel();
		pan.setLayout(new GridLayout(0,2));
		pan.add(labelOpt);
		pan.add(jcbChoixOpt);
		pan.add(labelX1);
		pan.add(jtfX1);
		pan.add(labelX2);				
		pan.add(jtfX2);
		
		boxH1 = Box.createHorizontalBox();
		boxH1.add(Box.createHorizontalStrut(15));
		boxH1.add(pan);
		boxH1.add(Box.createHorizontalStrut(15));
		
		boxH2 = Box.createHorizontalBox();
		boxH2.add(Box.createHorizontalStrut(15));
		boxH2.add(bOk);
		boxH2.add(Box.createHorizontalStrut(15));
		boxH2.add(bAnnuler);
		boxH2.add(Box.createHorizontalStrut(15));
		
		boxV = Box.createVerticalBox();
		boxV.add(Box.createHorizontalStrut(50));
		boxV.add(boxH1);
		boxV.add(Box.createHorizontalStrut(30));
		boxV.add(boxH2);
		boxV.add(Box.createHorizontalStrut(50));
		
		this.setPreferredSize(new Dimension(195,130));
		this.setLayout(new BorderLayout());
		this.add(boxV,BorderLayout.CENTER);
		
		fen = new JDialog();
		fen.getRootPane().setDefaultButton(bOk);
		fen.add(this);
		fen.setTitle("Fonction à optimiser");
		fen.setLocation(400,320);
		fen.setResizable(false);
		fen.setAlwaysOnTop(true);
		fen.setModal(true);
		fen.pack();
		fen.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{	
		String cmd = e.getActionCommand();
		
		if(cmd.equals("annuler"))
		{
			fen.dispose();
		}
		
		if(cmd.equals("ok"))
		{
			float x1 = Float.parseFloat(jtfX1.getText());
			float x2 = Float.parseFloat(jtfX2.getText());
			
			if(jcbChoixOpt.getSelectedItem().equals("Minimiser"))
			{
				z1.charger2D(null,new FonctionAOptimiser2D(x1,x2,false));
			}
			if(jcbChoixOpt.getSelectedItem().equals("Maximiser"))
			{
				z1.charger2D(null,new FonctionAOptimiser2D(x1,x2,true));
			}
			fen.dispose();
		}
	}
}